package com.dd.stack.redis.config.redisConfig;

import com.dd.stack.redis.config.redisConfig.constants.ChannelNameConstants;
import com.dd.stack.redis.config.redisConfig.msgListener.OrderMessageListener;
import com.dd.stack.redis.config.redisConfig.msgListener.RedisMessageListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;

/**
 * @Author liuxianmeng
 * @CreateTime 2025/1/30 16:56
 * @Description 配置Redis的订阅功能，将消息监听器绑定到特定的频道
 */
@Configuration
public class RedisPubSubConfig {

    @Autowired
    private RedisConnectionFactory rcf;
    @Autowired
    private RedisMessageListener rml;
    @Autowired
    private OrderMessageListener oml;

    @Bean
    public RedisMessageListenerContainer redisContainer() {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(rcf);

        // 绑定监听器到频道
        container.addMessageListener(rml, new PatternTopic(ChannelNameConstants.CHANNEL_ORDER));
        container.addMessageListener(oml, new PatternTopic(ChannelNameConstants.CHANNEL_ORDER_V2));
        return container;
    }
}
